local xiongluan = fk.CreateSkill {
  name = "lvshi__xiongluan",
}

Fk:loadTranslationTable{
  ["lvshi__xiongluan"] = "雄乱",
  [":lvshi__xiongluan"] = "每轮每身份限一次，其他角色的回合结束后，你可移去并使用中央区内的一张伤害类牌，当其他角色受到以此法造成的伤害后，其可移去并使用中央区内的一张【杀】。",
  ["#lvshi__xiongluan-use"] = "你可移去并使用中央区内的一张伤害类牌，",
  ["#lvshi__xiongluan-slash"] = "你可移去并使用中央区内的一张【杀】",

  ["$lvshi__xiongluan1"] = "吾为一方枭雄，怎会惧你。",
  ["$lvshi__xiongluan2"] = "这乱世，我定可取得一地之位。",
}

local DIY = require "packages/diy_utility/diy_utility"

xiongluan:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(xiongluan.name) and target ~= player and target.role_shown then
      local roles = player:getTableMark("lvshi__xiongluan-round")
      local role = target.role
      if role == "rebel_chief" then role = "rebel" end
      local cards = table.filter(DIY.getCenterCards(player.room), function (id)
        return Fk:getCardById(id).is_damage_card
      end)
      return #cards > 0 and not table.contains(roles, role)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(DIY.getCenterCards(room), function (id) return Fk:getCardById(id).is_damage_card end)
    local use = room:askToUseRealCard(player, {
      pattern = cards,
      skill_name = xiongluan.name,
      prompt = "#lvshi__xiongluan-use",
      extra_data = { expand_pile = cards },
      skip = true,
    })
    if use then
      event:setCostData(self, {use = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local role = target.role
    if role == "rebel_chief" then role = "rebel" end
    room:addTableMark(player, "lvshi__xiongluan-round", role)
    local use = event:getCostData(self).use
    if use then
      local id = use.card:getEffectiveId()
      DIY.removeCenterCards(player, id)
      use.card.skillName = xiongluan.name
      room:useCard(use)
    end
  end,
})

xiongluan:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xiongluan.name, true, true) and data.card and data.card.skillName == xiongluan.name
    and not target.dead
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(DIY.getCenterCards(room), function (id) return Fk:getCardById(id).trueName == "slash" end)
    if #cards == 0 then return end
    local use = room:askToUseRealCard(target, {
      pattern = cards,
      skill_name = xiongluan.name,
      prompt = "#lvshi__xiongluan-slash",
      extra_data = { expand_pile = cards },
      skip = true,
    })
    if use then
      event:setCostData(self, {use = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).use
    if use then
      local id = use.card:getEffectiveId()
      DIY.removeCenterCards(player, id)
      use.card.skillName = xiongluan.name
      room:useCard(use)
    end
  end,
})

xiongluan:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return xiongluan